// import { useEffect, useState } from "react";
import { useSearchParams } from "react-router-dom";
import { useRequest } from "ahooks";
import {
  LIST_SEARCH_PARAM_KEY,
  LIST_PAGE_PARAM_KEY,
  LIST_PAGE_SIZE_PARAM_KEY,
  LIST_PAGE_SIZE,
} from "../constant";
import { getQuestionListServices } from "../services/question";

type TypeOption = {
  isStar: boolean;
  isDeleted: boolean;
};
function useLoadQuestionListData(opt: Partial<TypeOption> = {}) {
  const { isDeleted = false, isStar = false } = opt;

  const [searchParams] = useSearchParams();
  console.log(searchParams.get("keyword"));
  const { data, loading, error,refresh } = useRequest(
    async () => {
      const keyword = searchParams.get(LIST_SEARCH_PARAM_KEY) || "";
      const page = parseInt(searchParams.get(LIST_PAGE_PARAM_KEY) || "") || 1;
      const pageSize =
        parseInt(searchParams.get(LIST_PAGE_SIZE_PARAM_KEY) || "") ||
        LIST_PAGE_SIZE;
      const res = await getQuestionListServices({
        keyword,
        isDeleted,
        isStar,
        pageSize,
        page,
      });
      return res;
    },
    {
      refreshDeps: [searchParams],
    }
  );
  return {
    data,
    loading,
    error,
    refresh
  };
}

export default useLoadQuestionListData;
